package com.concerneyes.glasses.task;

import java.util.HashMap;

import com.concerneyes.CEApp;
import com.concerneyes.R;
import com.concerneyes.glasses.model.GlassesDetailModel;
import com.concerneyes.glasses.model.GlassesItemModel;
import com.google.gson.Gson;

import android.content.Context;

import http.HttpAsyncTask;
import http.JsonMap;
import http.Url;
import util.ToastHelper;

/**
 * 产品相关网络请求
 */
public class GlassesImpl {
    private Context context;
    private GlassesListener listener;

    public GlassesImpl(GlassesListener listener) {
        this.listener = listener;
        this.context = CEApp.getInstance();
    }

    /**
     * 产品分类列表
     */
    public void categoryList() {
        HttpAsyncTask http = new HttpAsyncTask() {

            @Override
            public void onStart(String taskId) {
            }

            @Override
            public void onFinish(String taskId, String response) {
                JsonMap data = JsonMap.parseJson(response);
                if (data == null) {
                    ToastHelper.getInstance()._toast(R.string.network_error_tip);
                    if (listener != null) {
                        listener.onCategoryListFail();
                    }
                    return;
                }

                if (data.getInt("code") == 200) {
                    if (listener != null) {
                        listener.onCategoryListSuccess(data.getListMap("data"));
                    }
                } else {
                    ToastHelper.getInstance()._toast(data.getString("msg"));
                    if (listener != null) {
                        listener.onCategoryListFail();
                    }
                }
            }
        };
        http.setDebug(true);
        http.excute(Url.BaseUrl + "/product/categoryList", null, "GET");
    }

    /**
     * 产品列表
     *
     * @param categoryId 产品分类ID
     * @param page       第几页
     * @param pageNum    每页请求数量
     */
    public void glassesList(int categoryId, int page, String pageNum) {
        HashMap<String, String> params = new HashMap<String, String>();
        params.put("categoryId", String.valueOf(categoryId));
        params.put("page", String.valueOf(page));
        params.put("pageNum", pageNum);
        HttpAsyncTask http = new HttpAsyncTask() {

            @Override
            public void onStart(String taskId) {
            }

            @Override
            public void onFinish(String taskId, String response) {
                JsonMap data = JsonMap.parseJson(response);
                if (data == null) {
                    if (listener != null) {
                        ToastHelper.getInstance()._toast(R.string.network_error_tip);
                        listener.onGlassesListFail();
                    }
                    return;
                }

                if (data.getInt("code") == 200) {
                    String datas = data.getMap("data").toString();
                    try {
                        GlassesItemModel model = new Gson().fromJson(datas, GlassesItemModel.class);
                        if (listener != null) {
                            listener.onGlassesListSuccess(model);
                        }
                    } catch (Exception e) {
                        listener.onGlassesListFail();
                    }
                } else {
                    ToastHelper.getInstance()._toast(data.getString("msg"));
                    if (listener != null) {
                        listener.onGlassesListFail();
                    }
                }
            }
        };
        http.setDebug(true);
        http.excute(Url.BaseUrl + "/product/search", params, "POST");
    }

    /**
     * 产品详情
     *
     * @param productNo 产品id
     * @param colorCode 产品默认颜色
     */
    public void glassesInfo(String productNo, String colorCode) {
        HashMap<String, String> params = new HashMap<String, String>();
        params.put("productNo", productNo);
        params.put("colorCode", colorCode);
        HttpAsyncTask http = new HttpAsyncTask() {

            @Override
            public void onStart(String taskId) {
            }

            @Override
            public void onFinish(String taskId, String response) {
                JsonMap data = JsonMap.parseJson(response);
                if (data == null) {
                    if (listener != null) {
                        ToastHelper.getInstance()._toast(R.string.network_error_tip);
                        listener.onGlassesInfoFail();
                    }
                    return;
                }

                if (data.getInt("code") == 200) {
                    String datas = data.getMap("data").toString();
                    try {
                        GlassesDetailModel model = new Gson().fromJson(datas, GlassesDetailModel.class);
                        if (listener != null) {
                            listener.onGlassesInfoSuccess(model);
                        }
                    } catch (Exception e) {
                        listener.onGlassesListFail();
                    }
                } else {
                    ToastHelper.getInstance()._toast(data.getString("msg"));
                    if (listener != null) {
                        listener.onGlassesInfoFail();
                    }
                }
            }
        };
        http.setDebug(true);
        http.excute(Url.BaseUrl + "/product/details", params, "POST");
    }


}
